import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import md5 from 'js-md5'
import user1 from '../../api/user'
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    site: '', //分中心编码
    sitename: '',
    subjectPkId: '',
    subject:'',
    isSystemAdministrator:''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_Sys:(state,data)=>
    {
      state.isSystemAdministrator = data
    },
    // SET_SITE: (state, site) => {
    //   state.site = site //这里site写分中心名称
    // },
    // SET_SITENAME: (state, sitename) => {
    //   state.sitename = sitename //这里site写分中心名称
    // },
    set_subjectPkId: (state, subjectPkId) => {
      state.subjectPkId = subjectPkId //这里site写分中心名称
    },
    set_subject: (state, subject) => {
      state.subject = subject //这里site写分中心名称
    },

  },
  getters: {
    token: state => state.token,
    name: state => state.name,
    avatar: state => state.avatar,
    roles: state => state.roles,
    //site: state => state.site,
    //sitename: state => state.sitename,
    subjectPkId: state => state.subjectPkId,
    subject: state => state.subject,
    isSystemAdministrator:state=>state.isSystemAdministrator
  },
  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      //就改了这里
      return new Promise((resolve, reject) => {
        // console.log(md5(userInfo.password))
        login(username, md5(userInfo.password)).then(response => {
        //e10adc3949ba59abbe56e057f20f883e
          //login(username, userInfo.password).then(response => {
          const data = response.data
          // console.log("登陆！")
          // console.log(data)
          setToken('1')
          commit('SET_TOKEN', "1")
          commit('SET_ROLES', data.roleList)
          commit('SET_NAME', data.userNum)
          if(data.userType==0)
          {
            commit('SET_Sys',true)
          }
          else
          {
            commit('SET_Sys',false)
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
      //      setToken("test")
      //      commit('SET_TOKEN', "test")
    },

    Jump({ commit }, userInfo) {
      const username = userInfo.userNum.trim()
      //就改了这里
      return new Promise((resolve, reject) => {
        try{
          console.log(userInfo)
          setToken('1')
          commit('SET_TOKEN', "1")
          commit('SET_ROLES',userInfo.roleList)
          commit('SET_NAME', userInfo.userNum)
          if(userInfo.userType === 0)
          {
            commit('SET_Sys',true)
          }
          else
          {
            commit('SET_Sys',false)
          }
          resolve()
        }catch(e) {
          reject(e)
        }

    })
      //      setToken("test")
      //      commit('SET_TOKEN', "test")
    },


    // 获取用户信息
    GetInfo({ commit, state }) {

      return new Promise((resolve, reject) => {
        // getInfo(state.token).then(response => {
        //   const data = response.data
        //   if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
        //     commit('SET_ROLES', data.roles)
        //   } else {
        //     reject('getInfo: roles must be a non-null array !')
        //   }
        //   commit('SET_NAME', data.name)
        //   commit('SET_AVATAR', data.avatar)
        //   resolve(response)
        // }

        user1.getUserInfo().then( response =>{
          // console.log("用户信息")
          // console.log(response.data)
          let data = response.data

          if (data.roleList.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roleList)

          } else {
            if(data.userType!=0)
            reject('getInfo: roles must be a non-null array !')
          }

          if(data.userType==0)
          {
            commit('SET_Sys',true)
          }
          else
          {
            commit('SET_Sys',false)
          }

         // commit('SET_ROLES', data.roleList)
          commit('SET_NAME', data.userNum)
          resolve(response)
        }

        ).catch(error => {
          // reject(error)
          reject("获取用户信息失败，请重新登录")
        })
      })

      //commit('SET_AVATAR', "null")

      //commit('SET_SITE', "001")
      //commit('SET_SITENAME',"sitename")
      //commit('set_subjectPkId',"001")
      //commit('set_subject',0)
      // console.log("显示store值")
      // console.log(this.$store.getters.roles)
      // console.log(this.$store.getters.name)

    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_Sys',false)
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
